﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Newtonsoft.Json;
using System.IO;
using System.Reflection;
using Newtonsoft.Json.Linq;
using System.Text;

namespace Cms.Web.CmsSystem.SysAdmin.FormSetting
{
    public partial class Parse : System.Web.UI.Page
    {
        public object ParseControl(Newtonsoft.Json.Linq.JObject jobject)
        {
            BLL.Controls.ControlType ctrlType = BLL.Controls.ControlType.None;
            foreach (Newtonsoft.Json.Linq.JProperty property in jobject.Properties())
            {
                if (property.Name.Equals("type", StringComparison.CurrentCultureIgnoreCase))
                {
                    ctrlType = (BLL.Controls.ControlType)Enum.Parse(ctrlType.GetType(), property.Value.ToString(), true);
                    break;
                }
            }
            Assembly assembly = Assembly.GetAssembly(typeof(BLL.Controls.BaseControl));
            if (assembly != null)
            {
                string ClassName = String.Format("Cms.BLL.Controls.{0}", ctrlType);
                Type controlType = assembly.GetType(ClassName);
                object obj = assembly.CreateInstance(ClassName, true);
                if (obj != null)
                {
                    foreach (PropertyInfo pi in controlType.GetProperties())
                    {
                        JToken jtoken = jobject[pi.Name.ToLower()];

                        if (jtoken != null)
                            pi.SetValue(obj, (object)jtoken.ToString(), null);
                    }
                }

            }

            return null;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string FormId = EntLib.Request.GetString("formid");
            string RecordId = EntLib.Request.GetString("id");
            string Action = EntLib.Request.GetString("act");
            BLL.Sys_Selfform bll = new BLL.Sys_Selfform();
            Model.Sys_Selfform model = bll.GetById(FormId, false);
            if (model != null && model.Layout.Count > 0)
            {
                ctrljson = model.Layout.First().Jsondata;
            }
            else
                ctrljson = "{}";
            if (String.IsNullOrEmpty(RecordId))
            {
                RecordId = System.Guid.NewGuid().ToString();
            }
            if (String.IsNullOrEmpty(Action))
            {
                Action = "update";
            }
            form1.Action = String.Format("FormHandler.ashx?formid={0}&action={1}&id={2}", FormId, Action, RecordId);
        }
        public string ctrljson { get; set; }
        public string ctrltype
        {
            get
            {
                return EntLib.Json.ToJson(ControlType);
            }
        }
        private static Dictionary<string, int> _ControlType = null;
        private static object objlock = new object();
        private static Dictionary<string, int> ControlType
        {
            get
            {
                if (_ControlType == null)
                    lock (objlock)
                    {
                        _ControlType = EntLib.TypeParse.GetEnumMember(typeof(Cms.BLL.Controls.ControlType));
                    }
                return _ControlType;
            }
        }
    }
}